package org.nanotek.lucene.query;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.nanotek.cms.domain.lucene.Index;
import org.nanotek.util.Base;

@SuppressWarnings("serial")
public class QueryContext implements Base<Long> {

	private Long id;
	private String searchPhrase;
	private Integer maxHits;
	private Map<ContextKeys, Base<?>> contextVariables;

	public QueryContext() {
		//TODO remember why was choose the concurrent hash map,
		// Just got is ... Context will be bound on several threads. thatis the need. the army concept...
		contextVariables = new ConcurrentHashMap<ContextKeys,Base<?>>();
	}

	@Override
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}


	public Integer getMaxHits() {
		if (maxHits == null || maxHits == 0)
			maxHits = ContextKeys.DEFAULT_RESULT_SIZE;
		return maxHits;
	}

	public void setMaxHits(Integer maxHits) {
		this.maxHits = maxHits;
	}

	public String getSearchPhrase() {
		return searchPhrase;
	}

	public void setSearchPhrase(String searchPhrase) {
		this.searchPhrase = searchPhrase;
	}

	public  Base<?>  get(ContextKeys key) {
		return contextVariables.get(key);
	}

	public Base<?> put(ContextKeys key, Base<?> value) {
		return contextVariables.put(key, value);
	}

	public Base<?> remove(String key) {
		return contextVariables.remove(key);
	}

	public QueryContext copy()
	{
		QueryContext context = new QueryContext();
		context.setSearchPhrase(this.getSearchPhrase());
		context.setMaxHits(this.getMaxHits());
		context.put(ContextKeys.CURRENT_INDEX, (Index)this.get(ContextKeys.CURRENT_INDEX));
//		context.put(ContextKeys.TARGET_FIELD, (Field)this.get(ContextKeys.TARGET_FIELD));
		return context;
	}

}
